package ${package}.${moduleName}.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ${package}.${moduleName}.dto.${className}DTO;
import ${package}.${moduleName}.service.${className}Service;
import me.dwliu.framework.common.model.PageData;
import me.dwliu.framework.common.model.Result;
import me.dwliu.framework.common.validator.ValidatorUtils;
import me.dwliu.framework.common.validator.group.CreateGroup;
import me.dwliu.framework.common.validator.group.UpdateGroup;
import me.dwliu.framework.core.log.annotation.LogAction;
import me.dwliu.framework.core.mybatis.constant.Constant;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.groups.Default;
import java.util.List;
import java.util.Map;


/**
 * ${comments!}
 *
 * @author ${author!} ${email!}
 * @date ${datetime!}
 **/
@RestController
@RequestMapping("${moduleName}/${pathName}")
@Api(value = "${comments!}", tags = {"${comments!}接口"})
@AllArgsConstructor
public class ${className}Controller {
    private final ${className}Service ${classname}Service;

    @GetMapping("page")
    @ApiOperation("分页")
    @LogAction("${comments!}分页")
    @PreAuthorize("hasAnyAuthority('${moduleName}:${pathName}:list')")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, defaultValue = "1", dataType = "int"),
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, defaultValue = "10", dataType = "int"),
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")
    })
    public Result<PageData<${className}DTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<${className}DTO> page = ${classname}Service.listEntityByPage(params);
        return Result.success(page);
    }

    @GetMapping
    @ApiOperation("列表")
    @LogAction("${comments!}列表")
    @ApiImplicitParams({
    })
    @PreAuthorize("hasAnyAuthority('${moduleName}:${pathName}:list')")
    public Result<List<${className}DTO>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        List<${className}DTO> list = ${classname}Service.listEntity(params);

        return Result.success(list);
    }

    @GetMapping("{id}")
    @ApiOperation("根据ID查询")
    @LogAction("${comments!}根据ID查询")
    @PreAuthorize("hasAnyAuthority('${moduleName}:${pathName}:detail')")
    public Result<${className}DTO> get(@PathVariable("id") Long id) {
        ${className}DTO data = ${classname}Service.getEntity(id);
        return Result.success(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogAction("${comments!}保存")
    @PreAuthorize("hasAnyAuthority('${moduleName}:${pathName}:save')")
    public Result save(@RequestBody ${className}DTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, CreateGroup.class, Default.class);

        ${classname}Service.insertEntity(dto);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogAction("${comments!}修改")
    @PreAuthorize("hasAnyAuthority('${moduleName}:${pathName}:update')")
    public Result update(@RequestBody ${className}DTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, Default.class);

        ${classname}Service.updateEntity(dto);

        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogAction("${comments!}删除")
    @PreAuthorize("hasAnyAuthority('${moduleName}:${pathName}:delete')")
    public Result delete(@RequestBody Long[] ids) {

        ${classname}Service.deleteBachEntity(ids);

        return Result.success();
    }

//	@GetMapping("export")
//	@ApiOperation("导出")
//	@LogAction("导出")
//  @PreAuthorize("hasAnyAuthority('${moduleName}:${pathName}:export')")
//	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
//		List<${className}DTO> list = ${classname}Service.list(params);
//
//		ExcelUtils.exportExcelToTarget(response, null, list, SysRoleExcel.class);
//	}



}

